﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Data;
using common4gp;
using System.Windows;
using System.Diagnostics.Contracts;

namespace wpf4gp.Converters
{

    /// <summary>
    /// Converts a <see cref="System.Boolean"/> value in a Visible/Hidden <see cref="System.Window.Visibility"/>
    /// </summary>
    public class BoolVisibleHiddenConverter : IValueConverter
    {

        /// <summary>
        /// Converts from <see cref="System.Boolean"/> to Visibile or Hidden <see cref="System.Window.Visibility"/>
        /// </summary>
        /// <returns>Visible if the bool values true, Hidden in the other cases</returns>
        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            Contract.Ensures(Contract.Result<object>().IsNotNull());
            Visibility result = Visibility.Hidden;
            if (value.IsNotNull())
            {
                bool? boolValue = value as bool?;
                if (boolValue.IsNotNull() &&
                    boolValue.HasValue &&
                    boolValue.Value)
                {
                    result = Visibility.Visible;
                }
            }
            return result;
        }


        /// <summary>
        /// Converts from <see cref="System.Window.Visibility"/> to <see cref="System.Boolean"/>
        /// </summary>
        /// <returns>True if is Visible, False in other cases</returns>
        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            Contract.Ensures(Contract.Result<object>().IsNotNull());
            bool result = false;
            if (value.IsNotNull())
            {
                Visibility? visibility = value as Visibility?;
                if (visibility.IsNotNull() &&
                    visibility.HasValue &&
                    visibility.Value == Visibility.Visible)
                {
                    result = true;
                }
            }
            return result;
        }
    }
}
